package cn.edu.scau.cmi.pengzhijie.client;



import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.pengzhijie.adapter.AdapterOfClass;
import cn.edu.scau.cmi.pengzhijie.adapter.AdapterOfObject;
import cn.edu.scau.cmi.pengzhijie.adapter.domain.Student;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import jdk.internal.org.objectweb.asm.util.CheckAnnotationAdapter;

public class AdapterClient extends Application{

	List<Student> studentlist = new ArrayList<Student>();
	List<Integer> numberlist = new ArrayList<Integer>();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		Group group = new Group();
		Scene scene = new Scene(group, 800, 800);
		primaryStage.setTitle("适配器模式");
		primaryStage.setScene(scene);
		primaryStage.show();
		ObservableList<String> options = FXCollections.observableArrayList("类适配器模式","对象适配器模式");
		ComboBox<String> comboBox = new ComboBox<>(options);
		comboBox.setValue("类适配器模式");
		Text nametext = new Text("学生姓名:");
		Text numbertext = new Text("学生学号:");
		nametext.setLayoutX(100);
		nametext.setLayoutY(50);
		numbertext.setLayoutX(400);
		numbertext.setLayoutY(50);
		TextField namefield = new TextField();
		TextField numberfield = new TextField();
		namefield.setLayoutX(160);
		namefield.setLayoutY(35);
		numberfield.setLayoutX(460);
		numberfield.setLayoutY(35);
		Button button = new Button("创建学生");
		button.setMinWidth(200);
		button.setLayoutX(300);
		button.setLayoutY(100);
		Label outputlabel = new Label();
		outputlabel.setLayoutX(200);
		outputlabel.setLayoutY(200);
		group.getChildren().add(nametext);
		group.getChildren().add(numbertext);
		group.getChildren().add(namefield);
		group.getChildren().add(numberfield);
		group.getChildren().add(button);
		group.getChildren().add(outputlabel);
		group.getChildren().add(comboBox);
		button.setOnAction(new EventHandler<ActionEvent>() {
			String str = "";
			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub

				if(comboBox.getItems().equals("类适配器模式")) {
					if(!numberfield.getText().isEmpty() && !namefield.getText().isEmpty()) {
						Student student = new Student();
						student.setName(namefield.getText());
						int number = Integer.parseInt(numberfield.getText());
						student.setNumber(number);

						boolean flag = false;
						for(Student student2: studentlist) {
							if(student2.getNumber() == number) {
								flag = true;
								break;
							}
						}
						if(flag == true) {
							str = "不能输入重复学号";
						} else {
							studentlist.add(student);
							numberlist.add(number);
							AdapterOfClass adapterOfClass = new AdapterOfClass();
							adapterOfClass.sortStudent(numberlist);
							//学生列表根据返回的排好序的学号排序
							for(Integer integer: numberlist) {
								for(Student student1: studentlist) {
									if(student1.getNumber() == integer) {	
										 str = str + "学生姓名：" + student1.getName() + "          			学生学号:" + student1.getNumber() + "\n";
									}
								}
							}
						}	
					
					} else {
						outputlabel.setText("请输入学生姓名和学生学号");
					}
					outputlabel.setText(str);
					str = "";
				} 
				else {
					if(!numberfield.getText().isEmpty() && !namefield.getText().isEmpty()) {
						Student student = new Student();
						student.setName(namefield.getText());
						int number = Integer.parseInt(numberfield.getText());
						student.setNumber(number);
						boolean flag = false;
						for(Student student2: studentlist) {
							if(student2.getNumber() == number) {
								flag = true;
								break;
							}
						}
						if(flag == true) {
							str = "不能输入重复学号";
						} else {
							studentlist.add(student);
							numberlist.add(number);
							AdapterOfObject adapterOfObject = new AdapterOfObject();
							adapterOfObject.sortStudent(numberlist);
							//学生列表根据返回的排好序的学号排序
							for(Integer integer: numberlist) {
								for(Student student1: studentlist) {
									if(student1.getNumber() == integer) {	
										 str = str + "学生姓名：" + student1.getName() + "          			学生学号:" + student1.getNumber() + "\n";
									}
								}
							}
						}	
					
					} else {
						outputlabel.setText("请输入学生姓名和学生学号");
					}
					outputlabel.setText(str);
					str = "";
				}
			
			}
		});
	}

}
